﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTO;

namespace DAO
{
    public class HangSanXuatDAO
    {
        #region 1   -   Queries

        public static HangSanXuatDTO LayHangSanXuatTheoID(int id)
        {
            MyDataBaseDataContext myDB = new MyDataBaseDataContext();
            HANG_SAN_XUAT temp = myDB.HANG_SAN_XUATs.Single(c => c.ID == id);
            if (temp != null)
                return new HangSanXuatDTO(temp.ID, temp.TenHangSanXuat, temp.DiaChi, new Uri("http://" + temp.Web));
            return null;
        }

        
        public static List<string> LayDanhSachTenHangSanXuat()
        {
            MyDataBaseDataContext myDB = new MyDataBaseDataContext();
            var listTenHSX = from hsx in myDB.HANG_SAN_XUATs
                             select hsx;
            List<string> result = new List<string>();
            foreach (var item in listTenHSX)
            {
                result.Add(item.TenHangSanXuat);
            }
            return result;
        }

        public static HangSanXuatDTO LayHangSanXuatTheoTenHangSanXuat(string tenhangsanxuat)
        {
            try
            {
                MyDataBaseDataContext myDB = new MyDataBaseDataContext();
                HANG_SAN_XUAT temp = myDB.HANG_SAN_XUATs.Single(c => c.TenHangSanXuat.CompareTo(tenhangsanxuat)==0);
                if (temp != null)
                    return new HangSanXuatDTO(temp.ID, temp.TenHangSanXuat, temp.DiaChi, new Uri("http://" + temp.Web));
                return null;
            }
            catch (Exception)
            {
                if (tenhangsanxuat.Contains("SAMSUNG"))
                    return LayHangSanXuatTheoTenHangSanXuatVaNhomSanPham(tenhangsanxuat, 1);
                else
                    return LayHangSanXuatTheoTenHangSanXuatVaNhomSanPham(tenhangsanxuat, 2);
            }
            return null;
            
        }

        public static HangSanXuatDTO LayHangSanXuatTheoTenHangSanXuatVaNhomSanPham(string tenhangsanxuat, int manhomdanhmucsanpham)
        {
            try
            {
                MyDataBaseDataContext myDB = new MyDataBaseDataContext();
                DANH_MUC_SAN_PHAM temp = myDB.DANH_MUC_SAN_PHAMs.Single(c => c.TenDanhMucSanPham == tenhangsanxuat &&
                    c.MaNhomDanhMucSanPham == manhomdanhmucsanpham);
                return HangSanXuatDAO.LayHangSanXuatTheoID(temp.ID);
            }
            catch (Exception)
            {

                return null;
            }
        }
        public static List<string> LayDanhSachTenHangSanXuatTheoNhomSanPham(int nhomsanpham)
        {
            MyDataBaseDataContext myDB = new MyDataBaseDataContext();
            var listTenHSX = from dmsp in myDB.DANH_MUC_SAN_PHAMs
                             where dmsp.MaNhomDanhMucSanPham == nhomsanpham
                             select dmsp;
            List<string> result = new List<string>();
            foreach (var item in listTenHSX)
            {
                result.Add(item.TenDanhMucSanPham);
            }
            return result;
        }

        public static bool ThemHangSanXuat(HangSanXuatDTO hangsanxuatDTO)
        {
            HANG_SAN_XUAT temp = ChuyenTuHangSanXuatDTOSangHangSanXuat(hangsanxuatDTO);
            MyDataBaseDataContext myDB = new MyDataBaseDataContext();
            try
            {
                myDB.HANG_SAN_XUATs.InsertOnSubmit(temp);
                myDB.SubmitChanges();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public static bool XoaHangSanXuatTheoID(int id)
        {
            MyDataBaseDataContext myDB = new MyDataBaseDataContext();
            HANG_SAN_XUAT temp = myDB.HANG_SAN_XUATs.Single(c => c.ID == id);
            try
            {
                myDB.HANG_SAN_XUATs.DeleteOnSubmit(temp);
                myDB.SubmitChanges();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public static bool XoaHangSanXuatTheoTenHangSanXuat(string tenhangsanxuat)
        {
            MyDataBaseDataContext myDB = new MyDataBaseDataContext();
            HANG_SAN_XUAT temp = myDB.HANG_SAN_XUATs.Single(c => c.TenHangSanXuat == tenhangsanxuat);
            try
            {
                myDB.HANG_SAN_XUATs.DeleteOnSubmit(temp);
                myDB.SubmitChanges();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        #endregion

        #region 2   -   Function Helper

        public static HANG_SAN_XUAT ChuyenTuHangSanXuatDTOSangHangSanXuat(HangSanXuatDTO hangsanxuatDTO)
        {
            HANG_SAN_XUAT result = new HANG_SAN_XUAT();
            result.ID = hangsanxuatDTO.ID;
            result.DiaChi = hangsanxuatDTO.DiaChi;
            result.TenHangSanXuat = hangsanxuatDTO.TenHangSanXuat;
            result.Web = hangsanxuatDTO.URL.ToString();
            return result;
        }

        #endregion
    }
}
